package com.javaee.term.service.iml;

import com.javaee.term.Enums.FinshStatusEnum;
import com.javaee.term.mapper.PurchaseInfoMapper;
import com.javaee.term.model.Product;
import com.javaee.term.model.PurchaseInfo;
import com.javaee.term.service.PurchaseInfoService;
import com.javaee.term.service.base.BaseServiceIml;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * author 陈伯瑞
 * date 2019/12/18 0018 12:50
 * version 1.0
 **/
@Service
public class PurchaseInfoServiceImpl extends BaseServiceIml implements PurchaseInfoService {
    @Override
    public PurchaseInfo findOne(Object id) {
        return purchaseInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<PurchaseInfo> findAll() {
        return purchaseInfoMapper.selectAll();
    }

    @Override
    public void delete(Object id) {
        purchaseInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(PurchaseInfo purchaseInfo) {
        return purchaseInfoMapper.insert(purchaseInfo);
    }

    @Override
    public int update(PurchaseInfo purchaseInfo) {
        return purchaseInfoMapper.updateByPrimaryKey(purchaseInfo);
    }

    @Override
    @Transactional
    public void surePurchaseAndUpdateStock(String purchaseId) {
        PurchaseInfo purchaseInfo = purchaseInfoMapper.selectByPrimaryKey(purchaseId);
        purchaseInfo.setFinshStatus(FinshStatusEnum.FINISHED.getCode());
        purchaseInfoMapper.updateByPrimaryKey(purchaseInfo);
        Product product = productMapper.selectByPrimaryKey(purchaseInfo.getProductId());
        product.setProductStock(product.getProductStock()+purchaseInfo.getProductQuantity());
        productMapper.updateByPrimaryKey(product);
    }
}
